<?php

class LogisticsHelper extends Helper {

    private $_field;
    private $_rule;

    public function __construct() {
        $this->_field = array('ls_oid', 'ls_express_name', 'ls_express_code', 'ls_express_no', 'ls_express_image', 'ls_dateline');
        $this->_rule = array(
            'ls_oid' => array('require', '对应客户不能为空'),
            'ls_express_no' => array('require', '快递单号不能为空')
        );
    }
    /**
     * 物流保存
     * @param type $data array('ls_express_no' => '', 'ls_express_image' => array(), 'ls_oid' => array())
     * @param type $where
     * @return type
     */
    public function logisticsSave($data) {        
        $validator = Validator::checkRule($data, $this->_rule);
        if (!$validator['pass']) {
            return array('success' => false, 'msg' => $validator['msg']);
        }
        $model = $this->LoadModel('Public');
        $model->beginTransaction();
        try{
            if(!empty($data['ls_oid'])){
                foreach($data['ls_oid'] as $k=>$v){
                    $logis_data = array(
                        'ls_oid' => $v,
                        'ls_express_no' => $data['ls_express_no'],
                        'ls_express_image' => serialize($data['ls_express_image']),
                        'ls_status' => 'ok',
                        'ls_dateline' => NOW_TIME
                    );
                    $id = $model->update('logistics', $logis_data, array('`ls_oid` = ?' => $v, '`ls_status` = ?' => 'no'));
                }
            }
            $model->commit();
            return array('success' => true, 'msg' => '处理成功');
        } catch (Exception $ex) {
            $model->rollBack();
            return array('success' => false, 'msg' => '保存异常:' . $ex->getMessage());
        }

    }
    /**
     * 更新
     * @param type $data
     * @param type $where
     * @return type
     */
    public function logisticsUpdate($data, $where){
        if (is_numeric($where)) {
            $where = array('`ls_id` = ?' => $where);
        }
        $model = $this->LoadModel('Public');
        $model->update('logistics', $data, $where);
        return array('success' => true, 'msg' => '处理成功'); 
    }
    /**
     * 物流数据
     * @param type $where
     * @return type
     */
    public function logisticsRow($where){
        if(is_numeric($where)){
            $where = array('`ls_id` = ?' => $where);
        }
        $model = $this->LoadModel('Public');
        $data = $model->getOneRow('logistics', $this->_field, $where);
        return $data;
    }
    
    public function logisticsAll($where){
        if(is_numeric($where)){
            $where = array('`ls_oid` = ?' => $where);
        }
        $model = $this->LoadModel('Public');
        $data = $model->getList('logistics', $this->_field, $where);
        return $data;
    }
    
}